﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WeiBeeCommon.Core;

namespace QunFaExpert
{
    public class RetweetActionManager
    {
        static private RetweetActionManager _manager;

        /// <summary>
        /// Get the singleton instance of the manager
        /// </summary>
        /// <returns>RewteetActionManager</returns>
        static public RetweetActionManager Instance()
        {
            if (_manager == null)
            {
                _manager = new RetweetActionManager();
            }
            return _manager;
        }

        /// <summary>
        /// Get the singleton instance of the manager with arguments
        /// </summary>
        /// <param name="selected">The selected WeiBee accounts to do the retweet</param>
        /// <param name="retweetId">The retweet post id</param>
        /// <param name="atList">The at name list, can be null</param>
        /// <param name="preAtText">The pre at text string, can be null</param>
        /// <param name="afterAtText">The after at text string, can be null</param>
        /// <returns></returns>
        static public RetweetActionManager Instance(List<AuthEntity> selected, string retweetId, List<string> atList, string preAtText, string afterAtText)
        {
            if (_manager == null)
            {
                _manager = new RetweetActionManager();
            }
            else
            {
                _manager.ClearUp();
            }
            _manager.WeiBeeEntityList = selected;
            _manager.RetweetId = retweetId;
            _manager.AtNames = atList;
            _manager.PreAt = preAtText;
            _manager.AfterAt = afterAtText;
            return _manager;
        }

        private RetweetActionManager() { }

        /// <summary>
        /// Set/Get WeiBee Entity List
        /// </summary>
        private List<AuthEntity> WeiBeeEntityList { set; get; }

        /// <summary>
        /// Get/Set Tweeter id for retweet
        /// </summary>
        private string RetweetId { set; get; }

        /// <summary>
        /// Get/Set sentence previous at
        /// </summary>
        private string PreAt { set; get; }

        /// <summary>
        /// Get/Set sentence after at.
        /// </summary>
        private string AfterAt { set; get; }

        /// <summary>
        /// Get/Set at name list
        /// </summary>
        private List<string> AtNames { set; get; }

        private int _currentAuthEnityIndex = 0;

        private List<DateTime> _lastRestDateTimes = null;
        private List<int> _retweetCounts = null;
        /// <summary>
        /// Get one retweet job
        ///     null, if no job available
        ///     non null, the job
        /// </summary>
        /// <returns>null, or RetweeJob</returns>
        public RetweetJob GetJob()
        {
            RetweetJob result = null;
            IWeiBee _currentRetweetWeiBee = null;

            if (string.IsNullOrEmpty(RetweetId))
            {
                return result;
            }
            if (WeiBeeEntityList == null)
            {
                return result;
            }
            if (WeiBeeEntityList.Count == 0)
            {
                return result;
            }
            _currentAuthEnityIndex = WeiBeeEntityList.FindIndex(e => e.ActTime <= DateTime.Now);
            if (_currentAuthEnityIndex == -1)
            {
                return result;
            }
            CheckInitRestDateTimes();
            CheckInitRetweetCounts();
            _currentRetweetWeiBee = WeiBeeEntityList[_currentAuthEnityIndex].GetWeiBee();
            result = new RetweetJob() { RetweetWeiBee = _currentRetweetWeiBee, RetweetId = this.RetweetId, AvailableTime = DateTime.Now, DisplayName = WeiBeeEntityList[_currentAuthEnityIndex].DisplayName };
            WeiBeeEntityList[_currentAuthEnityIndex].ActTime = DateTime.Now.AddSeconds((double)_currentRetweetWeiBee.GetRetweetIntervalNumber());
            _retweetCounts[_currentAuthEnityIndex]++;
            if (_retweetCounts[_currentAuthEnityIndex] == _currentRetweetWeiBee.GetTotalRetweetHourlyLimitNumber())
            {
                _lastRestDateTimes[_currentAuthEnityIndex] = DateTime.Now;
                WeiBeeEntityList[_currentAuthEnityIndex].ActTime = _currentRetweetWeiBee.GetResetTime();
                _retweetCounts[_currentAuthEnityIndex] = 0;
            }

            string statusResult;
            if (AtNames != null && AtNames.Count > 0)
            {
                statusResult = string.Format("{0}@{1} {2}", PreAt, AtNames[0], AfterAt);
                AtNames.RemoveAt(0);
            }
            else
            {
                statusResult = string.Format("{0}{1}", PreAt, AfterAt);
            }
            if (string.IsNullOrEmpty(statusResult))
            {
                statusResult = @"转发";
            }

            result.Status = statusResult;
            //result = new RetweetJob() { RetweetWeiBee = _currentRetweetWeiBee, RetweetId = this.RetweetId, Status = statusResult };
            return result;
        }

        private void CheckInitRetweetCounts()
        {
            if (_retweetCounts == null)
            {
                _retweetCounts = new List<int>();
                for (int j = 0; j < WeiBeeEntityList.Count; j++)
                {
                    _retweetCounts.Add(0);
                }
            }
        }

        private void CheckInitRestDateTimes()
        {
            if (_lastRestDateTimes == null)
            {
                _lastRestDateTimes = new List<DateTime>();
                for (int i = 0; i < WeiBeeEntityList.Count; i++)
                {
                    _lastRestDateTimes.Add(DateTime.Now);
                }
            }
        }

        internal void ClearUp()
        {
            _lastRestDateTimes = null;
            _retweetCounts = null;
            this.AfterAt = string.Empty;
            this.PreAt = string.Empty;
            this.AtNames = null;
            this.WeiBeeEntityList = null;
        }
    }

    public class RetweetJob
    {
        public IWeiBee RetweetWeiBee { set; get; }
        public string DisplayName { set; get; }
        public string RetweetId { set; get; }
        public string Status { set; get; }
        public DateTime AvailableTime { set; get; }
    }
}
